package main

import (
	"fmt"
)

func fbn(n int) int {
	if n < 2 {
		return 1
	}
	return fbn(n-1) + fbn(n-2)
}

//冒泡排序~沉底排序
func BubbleSort(s []int) []int {
	for j := 0; j < len(s)-1; j++ {
		for i := 0; i < len(s)-1-j; i++ {
			if s[i] > s[i+1] {
				s[i], s[i+1] = s[i+1], s[i]
			}
		}
	}
	return s
}

//二分查找,得先排序才能查找
func BinaryFind(s []int, lindex, rindex, v int) {
	if lindex > rindex {
		fmt.Println("查无此数")
		return
	}
	//找到中间位并比较
	m := (lindex + rindex) / 2
	if v > s[m] {
		BinaryFind(s, m+1, rindex, v)
	} else if v < s[m] {
		BinaryFind(s, lindex, m-1, v)
	} else {
		fmt.Println("数字下标为", m)
	}

}

//遍历二维数组
func slice2(s [][]int) {
	for i := 0; i < len(s); i++ {
		for j := 0; j < len(s[i]); j++ {
			fmt.Printf("%v\t", s[i][j])
		}
	}
	fmt.Println()
}

func slice3(s [][]int) {
	for i, v := range s {
		for j, v1 := range v {
			fmt.Printf("s[%v][%v] = %v\t", i, j, v1)
		}
	}
	fmt.Println()
}

//定义二维数组求x个班y名同学班平均分和学校平均分，要求每班人数相等
func slice4() {
	var x, y int
	fmt.Println("请输入班级数和每个班人数")
	fmt.Scanln(&x, &y)
	fmt.Println(x, y)
	var s [][]int
	fmt.Println("请依次输入成绩")
	for i := 0; i < x; i++ {
		var tmp []int
		for j := 0; j < y; j++ {
			var tmp1 int
			fmt.Scanln(&tmp1)
			tmp = append(tmp, tmp1)
		}
		s = append(s, tmp)
	}
	fmt.Println("成绩数据为", s)
	var sum int
	for i := 0; i < x; i++ {
		var sum1 int
		for j := 0; j < y; j++ {
			sum1 += s[i][j]
			sum += s[i][j]
		}
		fmt.Printf("%v班的平均成绩为%.2f\n", i+1, float64(sum1)/float64(y))
	}
	fmt.Printf("学校平均成绩为%.2f\n", float64(sum)/float64(x*y))

}

func main() {
	// var heroes [3]string
	// fmt.Println("请依次输入三个英雄名字:")
	// for i := 0; i < 3; i++ {
	// 	fmt.Scanln(&heroes[i])
	// }
	// for _, heroes := range heroes {
	// 	fmt.Printf(heroes)
	// }
	a := []int{1, 2, 3}
	fmt.Println(len(a), cap(a))
	a = append(a, 1)
	fmt.Println(len(a), cap(a))
	//超过数组长度，未超过容量不扩容，append不产生新的数组，
	b := make([]int, 3, 5)
	fmt.Println(b, len(b), cap(b), &b[0])
	b = append(b, 1)
	fmt.Println(b, len(b), cap(b), &b[0])
	b[0] = 5
	fmt.Println(b, len(b), cap(b), &b[0])
	b = append(b, 1)
	fmt.Println(b, len(b), cap(b), &b[0])
	b = append(b, 1)
	fmt.Println(b, len(b), cap(b), &b[0])
	//a的长度和容量不会改变，只copy长度内的数据
	copy(a, b)
	fmt.Println(a, b)
	fmt.Println(len(a), cap(a))
	str := "hello"
	str1 := str[:]
	fmt.Println(str1)
	fmt.Println(str1[0])
	//str1[0] = 'y'  报错
	str2 := []rune(str)
	fmt.Println(string(str2))
	str2[0] = 'y'
	fmt.Println(string(str2))
	var slice1 []int
	for i := 0; i < 10; i++ {
		slice1 = append(slice1, fbn(i))
	}
	fmt.Println(slice1)
	s := []int{1, 5, 6, 89, 12, 45, 3}
	fmt.Println(s)
	fmt.Println(BubbleSort(s))
	BinaryFind(s, 0, len(s), 15)
	s1 := [][]int{{1, 5}, {9, 12}}
	slice2(s1)
	slice3(s1)
	slice4()

}
